<?php

namespace app\admin\controller;

use think\Db;
use think\facade\Session;


class Admin extends Common
{

    protected $table = 'admin';

    public function index()
    {
        $data = Db::name($this->table)
            ->alias('a')->join($this->table . '_type at', 'a.type=at.id')
            ->field('a.id, a.type, a.name, a.city, at.title, a.create_time, a.create_ip, a.update_time, a.update_ip, a.hide,a.is_customer_service')
            ->paginate();
        $this->assign('data', $data);
        return $this->fetch();
    }

    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $validate = validate('Admin');
            if (!$validate->check($data)) {
                return ['err' => $validate->getError()];
            }
            $id = $data['id'];
            unset($data['id']);
            if (!empty($data['password'])) {
                $data['password'] = md5($data['password']);
                unset($data['password2']);
            } else {
                unset($data['password']);
                unset($data['password2']);
            }
            if (isset($data['city']) && $data['city'] == '') {
                unset($data['city']);
            }
            $adminId = Db::name($this->table)->where('name', $data['name'])->value('id');
            $data['update_time'] = time();
            $data['update_ip'] = $this->request->ip();
            if ($id) {
                if ($adminId && $id != $adminId) {
                    return ['err' => '账号已存在，请重新输入'];
                }
                Db::name($this->table)->where('id', $id)->update($data);

            } else {
                if ($adminId) {
                    return ['err' => '账号已存在，请重新输入'];
                }
                $data['create_ip'] = $data['update_ip'];
                $data['create_time'] = $data['update_time'];
                $id = Db::name('admin')->insert($data);
            }

            Db::name('admin')->where([['id', 'neq', $id]])->update(['is_customer_service' => 0]);


            if ($data['is_customer_service'] == 1) {
                $admin = Session::get('admin', 'mmjtAdmin'); //用户
                if ($id == $admin['id']) {
                    $admin = Db::name('admin')->where('id', $id)->find();
                    Session::set('admin', $admin, 'mmjtAdmin');
                }
            }


            return ['ret' => '保存成功'];
        }
        $id = input('get.id', 0);
        $admin_type = Db::name('admin_type')->field('id,title')->select();
        $this->assign('admintype', $admin_type);
        $name = Db::name('admin')->where('id', $id)->find();
        $this->assign('name', $name);
        return $this->fetch();
    }

    //删除管理员账户
    public function del()
    {
        if ($this->request->isAjax()) {

            $id = input('id');
            if ($id == 1) {
                $data = ['err' => '账户禁止删除'];
            } else {
                if (input('?id')) {
                    $db = Db::name($this->table)->delete($id);
                    if ($db) {
                        $data = ['ret' => '删除成功'];
                    } else {
                        $data = ['err' => '删除失败' . $db];
                    }
                } else {
                    $data = ['err' => 'id参数错误'];
                }
            }
        } else {
            $data = ['err' => '提交参数错误'];
        }
        return $data;
    }

    //权限与类型
    public function type()
    {
        $data = Db::name($this->table . '_type')->select();
        $this->assign('data', $data);
        return $this->fetch();
    }

    public function typeAdd()
    {
        if ($this->request->isAjax()) {
            $data = $this->request->post();
            if (!isset($data['id']) || !isset($data['title'])) {
                return ['err' => '缺少必要参数'];
            }
            if (empty($data['title'])) {
                return ['err' => '角色名称不能为空'];
            }
            if ($data['id'] != 1 && empty($data['data'])) {
                return ['err' => '至少赋予角色一个权限'];
            }
            $typeId = Db::name($this->table . '_type')->where('title', $data['title'])->value('id');
            if ($typeId && (empty($data['id']) || $typeId != $data['id'])) {
                return ['err' => '角色名称重复'];
            }
            $id = $data['id'];
            unset($data['id']);
            $data['data'] = json_encode($data['data']);
            if ($id > 0) {
                if (!Db::name($this->table . '_type')->where('id', $id)->update($data)) {
                    return ['err' => '更新失败，请重试'];
                }
            } else {
                if (!Db::name($this->table . '_type')->insert($data)) {
                    return ['err' => '保存失败，请重试'];
                }
            }
            return ['ret' => '保存成功'];
        }
        $data = $this->request->get();
        $category = $type = array();
        if (!empty($data['id'])) {
            $type = Db::name($this->table . '_type')->where('id', $data['id'])->find();
        }
        if (empty($data['id']) || $data['id'] > 1) {
            if (!empty($data['id'])) {
                if ($type) {
                    $category = json_decode($type['data'], true);
                }
            }
            $category = _getCategory($category);
        }
        $this->assign('name', $type);
        $this->assign('category', $category);
        return $this->fetch();
    }


}
